﻿using System;
using System.Configuration;

namespace MVCFoto.HttpHandler.Configuration
{

    public class ImageHandlerConfigSection: ConfigurationSection
    {
        /// <summary>
        /// 是否启用防盗链
        /// </summary>
        [ConfigurationProperty("enable", IsRequired = true)]       
        public bool Enable
        {
            get
            {
                return (bool)this["enable"];
            }
            set
            {
                this["enable"] = value;
            }
        }
        /// <summary>
        /// 是否允许直接访问
        /// </summary>
        [ConfigurationProperty("allowDirectAccess")]
        public bool AllowDirectAccess
        {
            get
            {
               
                    return (bool)this["allowDirectAccess"];           
            }
            set
            {
                this["allowDirectAccess"] = value;
            }
        }

        /// <summary>
        /// 检测到盗链后重定向url
        /// </summary>
        [ConfigurationProperty("redirect")]
        public string Redirect
        {
            get
            {
                if (!string.IsNullOrEmpty(this["redirect"].ToString()))
                    return (string)this["redirect"];
                else
                {
                    if (Enable)
                        throw new ConfigurationException("防盗链启用时必须指定redirect路径");
                    else
                        return "";
                }
            }
            set
            {
                this["redirect"] = value;
            }
        }

        [ConfigurationCollection(typeof(DomainElementCollection), AddItemName="add")]
        [ConfigurationProperty("allow")]
        public DomainElementCollection AllowDomains
        {
            get
            {
                return (DomainElementCollection)this["allow"];
            }
        }
        [ConfigurationCollection(typeof(DomainElementCollection), AddItemName = "add")]
        [ConfigurationProperty("deny")]
        public DomainElementCollection DenyDomains
        {
            get
            {
                return (DomainElementCollection)this["deny"];
            }
        } 
    }

}
